import {
  searchFormIf,
  addForm,
  deleteReq,
  editForm,
  searchTree,
  searchPermisstionList,
  detailsSearch,
  findPlatform,
  findAll,
  updateRole
} from '@/services/roleManageSer';
import {
  message,
} from 'antd';
// 处理 页面型时使用
const getCheckKey = (permisson)=>{

  if(permisson){
    let {checkKeyList ,halfCheckKeyList} =  getPermissId(permisson);
    return {
      checked : checkKeyList,
      halfChecked : halfCheckKeyList
    }
  }


}
const getPermissId =(permisson)=>{
  let checkKeyList = [];
  let halfCheckKeyList = [];

  permisson.map((item)=>{

    if(item.permissions && item.permissions.length>0){
      halfCheckKeyList.push(item.permission_id)
      //end=end.concat( this.state.halfCheckKey);

      const newCheckObj = getPermissId(item.permissions);
      checkKeyList =checkKeyList.concat (newCheckObj.checkKeyList);
      halfCheckKeyList = halfCheckKeyList.concat(newCheckObj.halfCheckKeyList);
    }else{
      checkKeyList.push(item.permission_id)
    }
  })
  return {
    checkKeyList : checkKeyList,
    halfCheckKeyList : halfCheckKeyList
  }
}

//处理 非页面型时使用
const getNoRoutePermissTree =(permissons) =>{
  const tree = []
  if(permissons && permissons.length>0){
    permissons.map((per,index)=>{
      if(per.permissions && per.permissions.length>0){ //说明有父菜单
        per.permission_name = per.permission_name+"-"+per.permissions[0].permission_name
        per.permissions = undefined;
        tree.push({
          ...per
        })
      }else {
        tree.push(per)
      }
    })
  }
  return tree;
}

export default {
  namespace: 'no_plateform_role',
  state: {
    checked_permissions:[],
    href_checked_permissions:[],
    updateRoleId:undefined,
  },
  effects: {
    *fetch ({payload}, { call, put }) {

      const roleList = yield call(searchFormIf, payload);
      yield put({
        type: 'save',
        payload: {
          list: roleList.list,
          pagination: {
            page_size: roleList.page_size,
            total_pages: roleList.total_pages,
            page_num: roleList.page_num,
          }
        },
      });
    },
    *deleteBegin({ payload, callback }, { call, put }) {
      const deleteNow = yield call(deleteReq, payload);
      if (callback && typeof callback === 'function') {
        callback (deleteNow);
      }
      yield put({
        type: 'searchEnd',
      });
    },
    *showUpdateModal({payload}, { call, put }) {

      if (payload.modalType === 'create'){
        const tenantRsponse = yield call(findAll, {});
        yield put({
          type: 'save',
          payload: {
            updateVisible :true,
            modalType:payload.modalType,
            tenantList :  tenantRsponse && tenantRsponse.return_code === 'SUCCESS' ? tenantRsponse.data:[]
          },
        });
      }else if (payload.modalType === 'update'){
        const data = yield call(detailsSearch, {role_id :payload.role_id});
        const tenantRsponse = yield call(findAll, {});
        yield put({
          type: 'save',
          payload: {
            updateVisible :true,
            modalType:payload.modalType,
            updateData:data,
            tenantList :  tenantRsponse && tenantRsponse.return_code === 'SUCCESS' ? tenantRsponse.data:[]
          },
        });
      }else  if (payload.modalType === 'detail'){

      }
    },
    *handleUpdateRole({payload}, { call, put }) {
      if (payload.modalType === 'create'){
        const response = yield call(addForm, {
          role_name:payload.role_name,
          description:payload.description,
          tenant_id:payload.tenant_id
        });
        if (response && response.return_code === 'SUCCESS') return true
      }else if (payload.modalType === 'update'){
        const response = yield call(updateRole, {
          role_name:payload.role_name,
          description:payload.description,
          role_id: payload.role_id,
          tenant_id:payload.tenant_id
        });
        if (response && response.return_code === 'SUCCESS') return true
      }
    },
    *showPermssionModal({payload}, { call, put }){
      if(payload.type === 'type_route'){
        const checkPermiss = yield call(searchPermisstionList, {role_id: payload.role_id,is_left_menu:'1,4'}); //查询该角色的菜单
        const perrmisstions = yield call(searchTree, {is_left_menu:'1,4'}); //查询所有的菜单
        const {checked ,halfChecked } = getCheckKey(checkPermiss.data);
        yield put({
          type: 'save',
          payload: {
            permissionVisible :true,
            updateRoleId:payload.role_id,
            permission:perrmisstions.data,
            checked_permissions:checked,
            href_checked_permissions : halfChecked,
          },
        });
      }else if (payload.type === 'type_component'){
        const perrmisstions = yield call(searchTree, {is_left_menu:'2,3'}); //查询所有的菜单
        const checkPermiss = yield call(searchPermisstionList, {role_id: payload.role_id,is_left_menu:'2,3'}); //查询该角色的菜单
        const checked = getNoRoutePermissTree(checkPermiss.data);

        yield put({
          type: 'save',
          payload: {
            permissionVisible :true,
            updateRoleId:payload.role_id,
            permission:perrmisstions.data,
            checked_permissions:checked,
            href_checked_permissions : [],
          },
        });
      }
    },
    *showDetail({payload}, { call, put }){
      const detailResponse = yield call(detailsSearch, {role_id: payload.role_id}); //查询角色详情
      const route_data = yield call(searchPermisstionList, {role_id: payload.role_id,is_left_menu:'1,4'}); //查询该角色的菜单
      const component_data = yield call(searchPermisstionList, {role_id: payload.role_id,is_left_menu:'2,3'}); //查询该角色的菜单
      yield put({
        type: 'save',
        payload: {
          detailVisible :true,
          detailInfo:detailResponse,
          detail_permission:{
            type_route:route_data.data,
            type_component:component_data.data
          },
        },
      });
    },
    *editBegin({ payload, callback }, { call, put }) {
      const editNow = yield call(editForm, payload);
      if(editNow && editNow.return_code === 'SUCCESS'){
        message.success('操作成功');
        return true;
      }
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  }
}